package autoTest.request;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.continuation.Continuation;
import org.eclipse.jetty.continuation.ContinuationSupport;

import autoTest.Holder;
import autoTest.Listener;
import autoTest.command.ErrorCode;
import autoTest.command.Params;
import autoTest.device.DeviceInfo;
import autoTest.device.Platform;
import autoTest.utils.TextUtils;
import main.ServerMain;
import net.sf.json.JSONObject;

public class GetTaskPart extends RequestPart {
	private static final long serialVersionUID = 2271797150647771295L;
	public GetTaskPart() {}
    
    @Override public void process(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    	final String device = req.getParameter(Params.DEVICE_ID);
    	String platform = req.getParameter(Params.PLAT_FORM);
    	System.out.println("[GetTaskPart] device=" + device);
    	System.out.println("[GetTaskPart] platform=" + platform);
    	if (TextUtils.isEmpty(device) || TextUtils.isEmpty(platform)) {		//请求参数不全，直接返回
    		doResponse(resp, 0, ErrorCode.ERR_PARAM_ERROR, null, null, null);
    	} else {
    		String requestId = platform + System.currentTimeMillis();		//记录当前设备最新的requestId
    		ServerMain.allDeviceReqMap.put(device, requestId);
    		
    		System.out.println("[GetTaskPart] requestId:" + requestId);
    		
    		Continuation continuation = ContinuationSupport.getContinuation(req);
    		Holder holder = new Holder();
    		holder.setContinuation(continuation);
    		ServerMain.allReponseMap.put(requestId, holder);
    		
    		DeviceInfo di = ServerMain.allDeviceInfo.get(device);			//记录当前设备信息
    		if (di == null) {
    			di = new DeviceInfo(device);
        		int type = Platform.valueStrOf(platform).value();
        		if (type == 0) {
        			di.setPlatform(Platform.ANDROID);
        		} else if (type == 1) {
        			di.setPlatform(Platform.IOS);
        		} else if (type == 2) {
        			di.setPlatform(Platform.WINDOWS);
        		} else {
        			di.setPlatform(null);
        		}
    		}
    		System.out.println("[GetTaskPart] step");
    		ServerMain.allDeviceInfo.put(device, di);
    		
    		holder.setListener(new Listener() {
				public void makeIt(int type, String msg, String taskId, String task) {
					try {
						System.out.println("[GetTaskPart] makeIt type=" + type + ",msg=" + msg + ",taskId=" + taskId +",task=" + task);
						if (type == 0) {
							doResponse(resp, type, ErrorCode.SUCCESS, msg, taskId, null);
						} else if (type == 1) {
							doResponse(resp, type, ErrorCode.SUCCESS, msg, taskId, task);
						} else {
							doResponse(resp, 0, ErrorCode.ERR_PARAM_ERROR, null, null, null);
						}
						ServerMain.allDeviceInfo.remove(device);		//返回数据后等待下次的链接
						System.out.println("[GetTaskPart] hold finish taskId=" + taskId);
					} catch (ServletException e) {
						System.out.println("[GetTaskPart] " + e.toString());
						e.printStackTrace();
					} catch (IOException e) {
						System.out.println("[GetTaskPart] " + e.toString());
						e.printStackTrace();
					}
				}
    		});
    		holder.getContinuation().suspend();
    	}
    }
    
    protected void doResponse(HttpServletResponse resp, int type, int code, String content, String taskId, String task) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");  
		resp.setContentType("text/plain");
		resp.setStatus(HttpServletResponse.SC_OK);
		PrintWriter pWriter = resp.getWriter();
		JSONObject joRet = new JSONObject();
		joRet.put(Params.CODE, code);
		joRet.put(Params.TYPE, type);
		joRet.put(Params.CONTENT, content);
		joRet.put(Params.TASK_ID, taskId);
		joRet.put(Params.CONTENT, content);
		if (type == 1) {
			joRet.put(Params.TASK_ID_DEL, task);
		}
		pWriter.println(joRet.toString()); 
	}
}
